package com.itheima.reggie.service.impl;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.util.ExportUtil;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.mapper.EmployeeMapper;
import com.itheima.reggie.service.EmplyeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

@Service
public class EmplyeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmplyeeService {

    @Autowired
    private EmployeeMapper employeeMapper;


    //修改员工数据的回显
    @Override
    public R updateEmployeeSelect(Long id) {
        //-------------使用mp
        //Employee employee = employeeMapper.selectById(id);
        //-------------使用mybatis
        Employee employee = employeeMapper.selectEmployeeById(id);
        return R.success(employee);
    }
    //导出员工数据
    @Override
    public R exportEmployee(HttpServletResponse response) {
        //获取员工数据
        List<Employee> employees = employeeMapper.selectList(null);
        //获取修改人名字与创建人名字
        HashMap<Long, String> updateUser = new HashMap<>();
        HashMap<Long, String> crateUser = new HashMap<>();
        for (Employee employee : employees) {
            //获取修改人id
            Long updateUserId = employee.getUpdateUser();
            //获取用户id
            Long employeeId = employee.getId();
            String updateName = employeeMapper.getUpdateNameById(updateUserId);
            //用用户id作为key,修改人做value
            updateUser.put(employeeId,updateName);
            Long createUserId = employee.getCreateUser();
           String createName = employeeMapper.getCreateUser(createUserId);
            crateUser.put(employeeId,createName);
        }
        ExportUtil.doIt(employees,response,updateUser,crateUser);
        return R.success("导出成功");
    }


    //修改员工信息
    @Override
    public R updateEmployee(Employee employee, HttpSession session) {
        //设置修改时间
        employee.setUpdateTime(LocalDateTime.now());
        //设置修改人
        Long employeeId = (Long) session.getAttribute("employee");
        employee.setUpdateUser(employeeId);
        //-------------------------------------------使用mp
        //employeeMapper.updateById(employee);
        //-------------------------------------------使用mybatis
        employeeMapper.updateEmployee(employee);
        return R.success("修改成功");
    }



    //员工分页
    @Override
    public R pageEmployee(Integer page, Integer pageSize, String name) {
/*        //使用mp分页查询
        Page<Employee> ipage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Employee> qw = new LambdaQueryWrapper<>();
        qw.like(name != null,Employee::getName,name);
        qw.orderByDesc(Employee::getUpdateTime);
        employeeMapper.selectPage(ipage,qw);
        //获取总记录数
        long total = ipage.getTotal();
        //获取列表数据
        List<Employee> records = ipage.getRecords();
        HashMap<String, Object> map = new HashMap<>();
        map.put("total",total);
        map.put("records",records);
        return R.success(map);*/
        //---------------------使用mybatis\
        int pageNo;
        if (page.intValue() == 0){
            pageNo = 0;
        }else {
            pageNo = (page - 1) * pageSize;
        }
        List<Employee> records = employeeMapper.employeePage(pageNo,pageSize,name);
     Integer total = employeeMapper.selectCountByEmployee();
        HashMap<String, Object> map = new HashMap<>();
        map.put("total",total);
        map.put("records",records);
        return R.success(map);
    }



    //新增员工
    @Override
    public R addEmployee(Employee employee, HttpSession session){
     //补全员工数据
        //设置密码
        String password = DigestUtils.md5DigestAsHex("123456".getBytes());
        employee.setPassword(password);
        //设置创建人和修改人
        Long employeeId = (Long) session.getAttribute("employee");
        employee.setCreateUser(employeeId);
        employee.setUpdateUser(employeeId);
        //设置创建时间与修改时间
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        //设置员工状态是否正常
        employee.setStatus(1);
        //调用mybatis新增员工
        //用hutool创建雪花算法
        Snowflake snowflake = IdUtil.getSnowflake(1, 1);
        long id = snowflake.nextId();
        employee.setId(id);
        employeeMapper.addEmployee(employee);
        //mp
        //employeeMapper.insert(employee);
       return R.success("添加成功");
    }


    //员工登录
    @Override
    public R<Employee> EmployeeLogin(Employee employee, HttpSession session) {
        //获取传入的用户名与密码
        String loginUsername = employee.getUsername();
        String loginPassword = employee.getPassword();
        //根据传入用户名从数据库获取对象
       Employee employee1 = employeeMapper.getEmployeeByUserName(loginUsername);
       if (employee1 == null){
           return R.error("用户不存在");
       }
       //获取密码
        String password = employee1.getPassword();
       //将传入密码加密
        String loginPasswordByMd5 = DigestUtils.md5DigestAsHex(loginPassword.getBytes());
        //对比
        if (password.equals(loginPasswordByMd5)){
            //去掉密码
            employee1.setPassword("");
            //存入session
            session.setAttribute("employee",employee1.getId());
            return R.success(employee1);
        }
        return R.error("密码错误");
    }
    //员工登出
    @Override
    public R EmployeeLogOut(HttpSession session) {
        session.removeAttribute("employee");
        return R.success("登出成功");
    }



}
